package com.hsck.order.kds.login

import android.view.LayoutInflater
import android.view.View
import com.hsck.order.kds.base.common.Constant
import com.hsck.order.kds.base.common.shortToast
import com.hsck.order.kds.base.utils.NetworkConnectListener
import com.hsck.order.kds.base.utils.NetworkUtil
import com.hsck.order.kds.base.isnoam.view.BaseActivity
import com.hsck.order.kds.databinding.ActivitySynchronizeBinding
import kotlinx.coroutines.*

class SynchronizeActivity : BaseActivity<ActivitySynchronizeBinding>() {

    private var updateJob: Job? = null
    private val coroutineScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
    override fun inflateBinding(inflater: LayoutInflater): ActivitySynchronizeBinding {
        return ActivitySynchronizeBinding.inflate(inflater)
    }

    override fun initData() {
        super.initData()
        if (NetworkUtil.isNetworkAvailable(this)) {
            Delay()
        } else {
            noNetwork()
        }

        binding.apply {
            btnSynchronizeRetry.setOnClickListener {
                if (NetworkUtil.isNetworkAvailable(this@SynchronizeActivity)) {
                    Delay()
                }else{
                    shortToast("请检查网络后刷新再试试")
                }
            }
            btnSynchronizeSynchronous.setOnClickListener {
                if (NetworkUtil.isNetworkAvailable(this@SynchronizeActivity)) {
                    Delay()
                }else{
                    noNetwork()
                }
            }
        }
    }

    private fun Delay() {
        binding.relSynchronizeLoading.visibility = View.VISIBLE
        binding.linSynchronizeNoNetwork.visibility = View.GONE
        binding.linSynchronizeNoData.visibility = View.GONE
        updateJob =   coroutineScope.launch {
            delay(3000)
                binding.relSynchronizeLoading.visibility = View.GONE
                binding.linSynchronizeNoNetwork.visibility = View.GONE
                binding.linSynchronizeNoData.visibility = View.VISIBLE
        }


    }

    override fun onDestroy() {
        super.onDestroy()
        if (updateJob!=null){
            updateJob?.cancel()
            updateJob =null
        }
    }

    /**
     * 无网络展示
     */
    private fun noNetwork() {
        binding.relSynchronizeLoading.visibility = View.GONE
        binding.linSynchronizeNoNetwork.visibility = View.VISIBLE
        binding.linSynchronizeNoData.visibility = View.GONE
    }

}